package com.example.fangwei.utilsapp.viewpager;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

/**
 * Created by fangwei on 2017/9/11.
 */

public class CustomerAdapter extends PagerAdapter {
    private List<Integer> mList;
    private Context context;
    private OnItemClickListener onItemClickListener;


    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public CustomerAdapter(List<Integer> list,Context context){
        mList = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mList.size() ;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    // 初始化显示的条目对象
    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        // return super.instantiateItem(container, position);
        // 准备显示的数据，一个简单的TextView
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(mList.get(position));
        // 添加到ViewPager容器
        container.addView(imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null){
                    onItemClickListener.onItemClick(position);
                }
            }
        });
        // 返回填充的View对象
        return imageView;
    }

    // 销毁条目对象
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // super.destroyItem(container, position, object);
        container.removeView((View) object);
    }


    public interface OnItemClickListener{
        void onItemClick(int position);
    }
}
